`
https://leetcode.cn/problems/friends-of-appropriate-ages/
`

/**
 * @param {number[]} ages
 * @return {number}
 */
var numFriendRequests = function (ages) {
  // 思路：https://leetcode.cn/problems/friends-of-appropriate-ages/solutions/2990994/ji-shu-hua-dong-chuang-kou-pythonjavaccg-jfya/
  const cnt = Array(121).fill(0)
  for (const age of ages) {
    cnt[age]++
  }

  let res = 0
  let cntWindow = 0
  let ageX = 0, ageY = 0

  while (ageX < cnt.length) {
    cntWindow += cnt[ageX]

    if (ageY * 2 <= ageX + 14) {
      // 不能发送好友请求
      cntWindow -= cnt[ageY++]
    }

    if (cntWindow > 0) {
      // 存在可以发送好友请求的用户
      res += cnt[ageX] * cntWindow - cnt[ageX]
    }
    ageX++
  }

  return res
};